const dice = {
    roll: function (value) {
        value += ""
        if (/[|,\\\/]/.test(value)) {
            return this.compete(value)
        } else if (/[d,D+-]/.test(value)) {
            return this.multi(value)
        } else {
            return this.decision(value)
        }
    },
    multi: function (value) {
        value += ""

        value = value.replaceAll("D", "d").replaceAll(" ", "")
        let result = 0
        value.matchAll(/[+,\-]?\d*d?\d+/g).forEach(function (v) {
            let value = v[0] + ""
            let nextAction = "+"
            if (/[+,\-]/.test(value)) {
                nextAction = value[0]
                value = value.replace(/[+,\-]/, "")
            }
            let diceResult = 0;
            if (/d/.test(value)) {
                let tmps = value.split("d")
                let diceNum = parseInt(tmps[0])
                let diceType = parseInt(tmps[1])
                diceResult += 1 + Math.floor(Math.random() * diceType)
                for (let i = 0; i < diceNum - 1; i++) {
                    diceResult += 1 + Math.floor(Math.random() * diceType)
                }
            } else {
                diceResult += parseInt(value)
            }
            switch (nextAction) {
                case "+": {
                    result += diceResult
                    break
                }
                case "-": {
                    result -= diceResult
                    break
                }
            }

        })
        return result
    },
    decision: function (value) {
        value += ""

        let ran = 1 + Math.floor(Math.random() * 100)
        if (ran > 95) {
            return "大失败:" + ran + "/" + value
        }
        if (ran < 6) {
            return "大成功:" + ran + "/" + value
        }
        if (ran <= value / 5) {
            return "极难成功:" + ran + "/" + value
        }
        if (ran <= value / 2) {
            return "困难成功:" + ran + "/" + value
        }
        if (ran <= value) {
            return "成功:" + ran + "/" + value
        }
        return "失败:" + ran + "/" + value
    },
    compete: function (value) {
        value += ""
        let sign = value.match(/[|,\\\/]/)[0]
        let tmps = value.split(/[|,\\\/]/)
        let A = parseInt(tmps[0])
        let B = parseInt(tmps[1])
        let resultA
        let ran = 1 + Math.floor(Math.random() * 100)
        let rankA;
        let rankB;
        if (ran > 95) {
            resultA = "大失败:" + ran + "/" + A
            rankA = 5
        } else if (ran < 6) {
            resultA = "大成功:" + ran + "/" + A
            rankA = 0
        } else if (ran <= A / 5) {
            resultA = "极难成功:" + ran + "/" + A
            rankA = 1
        } else if (ran <= A / 2) {
            resultA = "困难成功:" + ran + "/" + A
            rankA = 2
        } else if (ran <= A) {
            resultA = "成功:" + ran + "/" + A
            rankA = 3
        } else {
            resultA = "失败:" + ran + "/" + A
            rankA = 4
        }
        let resultB
        ran = 1 + Math.floor(Math.random() * 100)
        if (ran > 95) {
            resultB = "大失败:" + ran + "/" + B
            rankB = 5
        } else if (ran < 6) {
            resultB = "大成功:" + ran + "/" + B
            rankB = 0
        } else if (ran <= B / 5) {
            resultB = "极难成功:" + ran + "/" + B
            rankB = 1
        } else if (ran <= B / 2) {
            resultB = "困难成功:" + ran + "/" + B
            rankB = 2
        } else if (ran <= B) {
            resultB = "成功:" + ran + "/" + B
            rankB = 3
        } else {
            resultB = "失败:" + ran + "/" + B
            rankB = 4
        }
        if (rankA === rankB) {
            switch (sign) {
                case "\|": {
                    return resultA + "==" + resultB
                }
                case "/": {
                    return resultA + "=>" + resultB
                }
                case "\\": {
                    return resultA + "<=" + resultB
                }
            }
        } else if (rankA > rankB) {
            return resultA + "=>" + resultB
        } else {
            return resultA + "<=" + resultB
        }
    }
}
export default dice;
